// components/voice-input/index.js
var plugin = requirePlugin("WechatSI")
let manager = plugin.getRecordRecognitionManager()

Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    count: 0,
    voiceInputValue: '',
    isAction: false,
    currentRes:'',
    repeatCount:0,
    showVoiceText:false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    actionVoiceInput(e) {
      this.startVoice()
    },
    startAnimation(){
      if(!this.data.isAction){
        return
      }
      this.animate('#voiceInput', [{
          scale: [1, 1]
        },
        {
          scale: [1.3, 1.3]
        },
        {
          scale: [0.9, 0.9]
        },
        {
          scale: [1.2, 1.2]
        },
        {
          scale: [1, 1]
        },
      ], 1200, function () {
        return this.startAnimation()
      }.bind(this))
    },
    startVoice(){
      manager.start({
        duration: 20000,
        lang: "zh_CN"
      })
    },
    recognizeVoice(res){
      console.log("current result", res.result)
    },
    closeVoiceMask(){
      this.triggerEvent('closevoice')
    },
    toSetting(){
      wx.navigateTo({
        url: '/pages/voiceSetting/voiceSetting',
      })
    },
    contrastVoiceInput(){
      let value = this.data.voiceInputValue
      let startValue = wx.getStorageSync('startValue') || undefined
      let endValue = wx.getStorageSync('endValue') || undefined
      value = value.split(',').join('').split('。').join('')
      let startList = ['开始','开启','启动','打开',startValue]
      let endList = ['关闭','结束','停止',endValue]
      setTimeout(()=>{
        this.setData({
          showVoiceText:false
        })
      },3000)
      for(let i of startList){
        if(value.indexOf(i) !== -1){
          // 这是start
          console.log('检测到姐钥打开',i,value,startList)
          return this.triggerEvent('voicestart')
        }
      }
      for(let i of endList){
        if(value.indexOf(i) !== -1){
          // 这是end
          console.log('检测到姐钥关闭',i)
          return this.triggerEvent('voiceend')
        }
      }
    }
  },
  attached() {
    manager.onRecognize = function recognizeVoice(res){
      console.log("current result", res.result)
      // if(this.data.currentRes == res.result){
      //   this.setData({
      //     repeatCount:this.data.repeatCount + 1
      //   })
      //   if(this.data.repeatCount >= 2){
      //     console.log('录音终止')
      //     manager.stop()
      //   }
      // } else {
      //   this.setData({
      //     repeatCount:0
      //   })
      // }
      this.setData({
        currentRes:res.result
      })
      return manager.stop()
    }.bind(this)
    manager.onStop = function (res) {
      console.log("record file path", res.tempFilePath)
      console.log("result", res.result)
      this.setData({
        isAction:false
      })
      this.setData({
        voiceInputValue:res.result
      })
      this.contrastVoiceInput()
    }.bind(this)
    manager.onStart = function (res) {
      this.setData({
        isAction:true
      })
      this.startAnimation()
      this.setData({
        showVoiceText:true
      })
      console.log("成功开始录音识别", res)
    }.bind(this)
    manager.onError = function (res) {
      this.setData({
        isAction:false
      })
      console.error("error msg", res.msg)
    }.bind(this)
  }
})